﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sharp.Portal.ExtNet.FormField
{
    /// <summary>
    /// 下拉框字段
    /// </summary>
    public class DropDownListField : ElementField
    {
        /// <summary>
        /// 下拉框字段
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="dataSourceName"></param>
        /// <param name="displayField"></param>
        /// <param name="valueField"></param>
        public DropDownListField(string fieldName, string dataSourceName, string displayField, string valueField)
        {
            this.FieldName = fieldName;
            this.DataSourceName = dataSourceName;
            this.DisplayField = displayField;
            this.ValueField = valueField;
        }
        /// <summary>
        /// 数据源名称
        /// </summary>
        public string DataSourceName { get; set; }
        /// <summary>
        /// 显示字段
        /// </summary>
        public string DisplayField { get; set; }
        /// <summary>
        /// 值字段
        /// </summary>
        public string ValueField { get; set; }
        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="nonFirst"></param>
        /// <returns></returns>
        public override string Build(bool nonFirst = false)
        {
            if (this.IsBrowsing() && !this.KeepingXType)
            {
                return string.Format("x.DisplayFieldFor(m => m.{0}).Value2Text(Model.{0},Model.{1},\"{2}\",\"{3}\")", DataSourceName, FieldName, DisplayField, ValueField) + base.Build(nonFirst);
            }

            return string.Format("x.ComboBoxFor(m => m.{0}).DataSource(Model.{1},Model.{0},\"{2}\",\"{3}\")", FieldName, DataSourceName, DisplayField, ValueField) + base.Build(nonFirst);
        }
    }
}